﻿namespace EntityAccessImp;

public class AnnotationServices : IAnnotationServices
{
    public ISqlSugarClient DB { get; set; }

    public AnnotationServices(ISqlSugarClient sqlSugarClient)
    {
        DB = sqlSugarClient;
    }

    public async Task<GetResult<List<Annotation>>> GetRelativeAnnotationsAsync(int draftID)
    {
        try
        {
            var r = await DB
                .Queryable<Annotation>()
                .Where(it => it.OwnerID == draftID)
                .Select(it => new Annotation() { FeatureInWKT = it.FeatureInWKT.ToString() , ID=it.ID, OwnerID=it.OwnerID})
                .ToListAsync();
            return new GetResult<List<Annotation>>()
            {
                StatusCode = Code.Succeed,
                Message = "查询Annotations成功",
                ResultObject = r
            };
        }
        catch (Exception e)
        {
            return new GetResult<List<Annotation>>()
            {
                StatusCode = Code.DBError,
                Message = "查询Annotations失败",
                SourceException = e
            };
        }
    }

    public async Task<AddResult<int>> AddAnnotationsAsync(IEnumerable<Annotation> annotations)
    {
        try
        {
            var r = await DB
                .Insertable(annotations.ToList())
                .ExecuteCommandAsync();
            return new AddResult<int>()
            {
                StatusCode = Code.Succeed,
                Message = "添加Annotations成功",
            };
        }
        catch (Exception e)
        {
            return new AddResult<int>()
            {
                StatusCode = Code.DBError,
                Message = "添加Annotations失败",
                SourceException = e
            };
        }
    }

    public async Task<DeleteResult> DeleteAnnotationsAsync(IEnumerable<int> annotationIDs)
    {
        try
        {
            var list = annotationIDs.ToList();
            var temp = new List<Annotation>(list.Count);
            list.ForEach((id) => temp.Add(new Annotation() { ID = id }));
            var r = await DB
                .Deleteable(temp)
                .ExecuteCommandAsync();
            return new DeleteResult()
            {
                StatusCode = Code.Succeed,
                Message = "删除Annotations成功",
                ChangedColumnNum = r
            };
        }
        catch (Exception e)
        {
            return new DeleteResult()
            {
                StatusCode = Code.DBError,
                Message = "删除Annotations失败",
                SourceException = e
            };
        }
    }

    public async Task<GetResult<List<Annotation>>> GetAnnotationsAsync(IEnumerable<int> annotationIDs)
    {
        try
        {
            var r = await DB
                .Queryable<Annotation>()
                .Where(it => annotationIDs.Contains(it.ID))
                .ToListAsync();
            return new GetResult<List<Annotation>>()
            {
                StatusCode = Code.Succeed,
                Message = "查询Annotations成功",
                ResultObject = r
            };
        }
        catch (Exception e)
        {
            return new GetResult<List<Annotation>>()
            {
                StatusCode = Code.DBError,
                Message = "查询Annotations失败",
                SourceException = e
            };
        }
    }

}